package logic.service;

import dao.NotesDAO;
import entity.Notes;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import vo.IValueObject;
import vo.NotesVO;

/**
 *
 * @author Alexander
 */
public class NotesService implements IService {

    @Override
    public void persist(IValueObject vo, EntityManager em) {
        Notes entity = new Notes();
        entity.setId(((NotesVO) vo).getId());
        entity.setDescription(((NotesVO) vo).getDescription());

        //entity.setCalendar();

        new NotesDAO().persist(entity, em);
    }

    @Override
    public IValueObject find(Object id, EntityManager em) {
        Notes nota = (Notes) new NotesDAO().find(id, em);
        if (nota != null) {
            return nota.toVO();
        } else {
            return null;
        }
    }

    @Override
    public void update(IValueObject vo, EntityManager em) {
        Notes entity = new Notes();
        entity.setId(((NotesVO) vo).getId());
        entity.setDescription(((NotesVO) vo).getDescription());

        //entity.setCalendar();

        new NotesDAO().update(entity, em);
    }

    @Override
    public void delete(Object id, EntityManager em) {
        new NotesDAO().delete(id, em);
    }

    @Override
    public List<IValueObject> getList(EntityManager em) {
        List<IValueObject> notesVO = new ArrayList<>();
        for (Object note : new NotesDAO().getList(em)) {
            notesVO.add(((Notes) note).toVO());
        }
        return notesVO;
    }
}
